﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PhotovoltaicETL
{
    class PointCalc
    {
        internal static double Calc(string calType, ICollection<double> list1, ICollection<double> list2)
        {
            //throw new NotImplementedException();
            switch (calType)
            {
                case "求和":
                    return GetSum(list1, list2);
                case "求平均":
                    return GetAverage(list1, list2);
                case "求积":
                    return GetMux(list1, list2);
                case "比例分配":
                    return GetCheck(list1, list2);
                case "求和平方根":
                    return GetSumSquare(list1, list2);
                case "复制":
                    return GetCopy(list1, list2);
                default:
                    return double.MinValue;
            }
        }

        private static double GetCopy(ICollection<double> list1, ICollection<double> list2)
        {
            //throw new NotImplementedException();
            if ((list1.Count == 0 || list1 == null) && (list2.Count == 0 || list2 == null))
                return 0;
            else if (list1 == null || list1.Count == 0)
            {
                if (list2.Count > 0)
                    return list2.First();
                else
                    return 0;
            }
            else
            {
                if (list1.Count > 0)
                    return list1.First();
                else
                    return 0;
            }
        }

        private static double GetSumSquare(ICollection<double> list1, ICollection<double> list2)
        {
            if ((list1.Count == 0 || list1 == null) && (list2.Count == 0 || list2 == null))
                return 0;
            double res = 1;
            if (list1 == null || list1.Count == 0)
                res = (from c in list2 select c * c).Sum();
            //list2.ForEach((para) => { res = res + para * para; });
            if (list2 == null || list2.Count == 0)
                res = (from c in list2 select c * c).Sum();
            //list1.ForEach((para) => { res = res + para * para; });
            return Math.Sqrt(res);
        }

        private static double GetCheck(ICollection<double> list1, ICollection<double> list2)
        {
            if ((list1.Count == 0 || list1 == null) || (list2.Count == 0 || list2 == null))
                return 0;
            else
            {
                double temp = list2.Sum();
                return temp == 0 ? 0 : GetMux(list1, null) / list2.Sum();
            }
        }

        private static double GetMux(ICollection<double> list1, ICollection<double> list2)
        {
            if ((list1.Count == 0 || list1 == null) && (list2.Count == 0 || list2 == null))
                return 0;
            double res = 1;
            if (list1 == null || list1.Count == 0)
            {
                foreach (double value in list2)
                    res = res * value;
            }
            if (list2 == null || list2.Count == 0)
            {
                foreach (double value in list1)
                    res = res * value;
            }
            return res;

        }

        private static double GetAverage(ICollection<double> list1, ICollection<double> list2)
        {
            if ((list1.Count == 0 || list1 == null) && (list2.Count == 0 || list2 == null))
                return 0;
            else if (list1 == null || list1.Count == 0)
                return list2.Average();
            else if (list2 == null || list2.Count == 0)
                return list1.Average();
            else
            {
                return list1.Concat(list2).Average();
            }
        }

        private static double GetSum(ICollection<double> list1, ICollection<double> list2)
        {
            if ((list1.Count == 0 || list1 == null) && (list2.Count == 0 || list2 == null))
                return 0;
            else if (list1 == null || list1.Count == 0)
                return list2.Sum();
            else if (list2 == null || list2.Count == 0)
                return list1.Sum();
            else
                return list1.Sum() + list2.Sum();
        }
    }
}
